var MaterialProjectSelector = {
	getView : function(callback, flag) {
		var initPan = this.initPanel(flag);
		var tabGroupMgr = new Ext.WindowGroup();
		//前置窗口
		tabGroupMgr.zseed=99999;
		var e = new Ext.Window({
			title : "选择物资",
			iconCls : "menu-City-manage",
			width : 640,
			height : 480,
			manager: tabGroupMgr,
			layout : "fit",
			border : false,
			items : [initPan],
			modal : true,
			buttonAlign : "center",
			buttons : [{
						iconCls : "btn-ok",
						text : "确定",
						handler : function() {
							var materialIds = "";
							var materialNames = "";
							var nums = "";
							if(flag){
								var MaterialProjectSelectorGrid = Ext.getCmp("MaterialProjectSelectorGrid");
								var selections = MaterialProjectSelectorGrid.getSelectionModel().getSelections();
								for (var k = 0; k < m.length; k++) {
									if (k > 0) {
										materialIds += ",";
										materialNames = ",";
										nums = ",";
									}
									materialIds += m[k].data.id;
									materialNames += m[k].data.materialName;
									nums += m[k].data.num;
								}
							}else{
								var store = Ext.getCmp("selectedMaterialGrid").getStore();
								for (var i = 0; i < store.getCount(); i++) {
									if (i > 0) {
										materialIds += ",";
										materialNames = ",";
										nums = ",";
									}
									materialIds += store.getAt(i).data.id;
									materialNames += store.getAt(i).data.materialName;
									nums+= store.getAt(i).data.num;
								}
							}
							if (callback != null) {
								callback.call(this, materialIds,materialNames,nums);
							}
							e.close();
						}
					}, {
						text : "取消",
						iconCls : "btn-cancel",
						handler : function() {
							e.close();
						}
					}]
		});
		return e;
	},
	initPanel : function(flag){
		var projectRepertory = new Ext.tree.TreePanel({
			title : "物资选择器",
			region : "west",
			width : 200,
			height : 300,
			split : true,
			collapsible : true,
			autoScroll : true,
			bbar : new Ext.Toolbar({
				items : [{
							xtype : "button",
							iconCls : "btn-refresh",
							text : "刷新",
							handler : function() {
								projectRepertory.root.reload();
							}
						}, {
							xtype : "button",
							text : "展开",
							iconCls : "btn-expand",
							handler : function() {
								projectRepertory.expandAll();
							}
						}, {
							xtype : "button",
							text : "收起",
							iconCls : "btn-collapse",
							handler : function() {
								projectRepertory.collapseAll();
							}
						}]
			}),
			loader : new Ext.tree.TreeLoader({
				url : __ctxPath + "/project/getTreeOfProject.do",
				baseParams : {
					start : 0,
					limit : 100
				}
			}),
			root : {},
			rootVisible : false,
			listeners : {
				"click" : function(node) {
					if (node != null) {
						var grid = Ext.getCmp("MaterialProjectSelectorGrid");
						var store = grid.getStore();
						store.proxy.conn.url = __ctxPath + "/produce/listProjectRepertory.do";
						if (node.attributes.typeId != -1) {
							store.baseParams = {
								'Q_project.id_L_EQ' : node.attributes.proId
							};
						}
						store.reader = new Ext.data.JsonReader({
											root : "result",
											totalProperty : "totalCounts",
											fields : [{
													name : 'id',
													type : 'int'
												}, {name:'materialName',mapping:'material.materialName'}, 'num']
										});
						store.load({
							params : {
								start : 0,
								limit : 12
							}
						});
					}
				}
			}
		});
		var b = null;
		if (flag) {
			var b = new Ext.grid.CheckboxSelectionModel({
				singleSelect : true
			});
		} else {
			b = new Ext.grid.CheckboxSelectionModel();
		}
		var h = new Ext.grid.ColumnModel({
			columns : [b, new Ext.grid.RowNumberer(),{
						header : 'id',
						dataIndex : 'id',
						hidden : true
					}, {
						header : '物资名称',
						dataIndex : 'materialName'
					}, {
						header : '数量',
						dataIndex : 'num'
					}]
		});
		var g = new Ext.data.Store({
			proxy : new Ext.data.HttpProxy({
				url : __ctxPath + "/produce/listProjectRepertory.do"
			}),
			reader : new Ext.data.JsonReader({
				root : "result",
				totalProperty : "totalCounts",
				fields : [{
						name : 'id',
						type : 'int'
					}, {name:'materialName',mapping:'material.materialName'}, 'num']
			}),
			sortInfo : {
				field : "id",
				direction : "desc"
			},
			remoteSort : true
		});
		var d = new Ext.grid.GridPanel({
			id : "MaterialProjectSelectorGrid",
			autoWidth : false,
			height : 300,
			region : "center",
			title : "物资列表",
			store : g,
			shim : true,
			trackMouseOver : true,
			disableSelection : false,
			loadMask : true,
			cm : h,
			sm : b,
			viewConfig : {
				forceFit : true,
				enableRowBody : false,
				showPreview : false
			},
			bbar : new Ext.PagingToolbar({
				pageSize : 25,
				store : g,
				displayInfo : true,
				displayMsg : "当前显示从{0}至{1}， 共{2}条记录",
				emptyMsg : "当前没有记录"
			})
		});
		g.load({
			params : {
				start : 0,
				limit : 12
			}
		});
		var a = new Ext.FormPanel({
			width : 400,
			region : "north",
			id : "MaterialProjectSearchForm",
			height : 40,
			frame : false,
			border : false,
			layout : "hbox",
			layoutConfig : {
				padding : "5",
				align : "middle"
			},
			defaults : {
				xtype : "label",
				margins : {
					top : 0,
					right : 4,
					bottom : 4,
					left : 4
				}
			},
			items : [{
						text : "请输入查询条件:"
					}, {
						style : 'margin-top:5px;',
						xtype : 'label',
						text : '物资名称:'
					}, {
						name : 'Q_material.materialName_S_LK',
						xtype : 'textfield'
					}, {
						xtype : "button",
						text : "查询",
						iconCls : "search",
						handler : function() {
							var j = Ext.getCmp("MaterialProjectSearchForm");
							var k = Ext.getCmp("MaterialProjectSelectorGrid");
							if (j.getForm().isValid()) {
								j.getForm().submit({
									waitMsg : "正在提交查询",
									url : __ctxPath + "/produce/listProjectRepertory.do",
									success : function(m, n) {
										var l = Ext.util.JSON.decode(n.response.responseText);
										k.getStore().loadData(l);
									}
								});
							}
						}
					}]
		});
		var totalRepertory = new Ext.tree.TreePanel({
					id : "totalRepertory",
					iconCls : "role-user",
					title : "总部仓库",
					loader : new Ext.tree.TreeLoader({
								url : __ctxPath + "/system/getTreeOfTreeType.do?className=Material"
							}),
					root : {},
					rootVisible : false,
					listeners : {
						"click" : function(node) {
								if (node != null) {
									var grid = Ext.getCmp("MaterialProjectSelectorGrid");
									var store = grid.getStore();
									store.proxy.conn.url = __ctxPath + "/produce/listMaterial.do";
									if (node.attributes.typeId != -1) {
										store.baseParams = {
											'Q_treeType.id_L_EQ' : node.attributes.typeId
										};
									}
									store.reader = new Ext.data.JsonReader({
										root : "result",
										totalProperty : "totalCounts",
										fields : [{
												name : 'id',
												type : 'int'
											}, 'materialName', 'num']
									}),
									store.load({
										params : {
											start : 0,
											limit : 12
										}
									});
								}
							}
					}
		});
		var eastPanel = new Ext.Panel({
					id : "eastPanel",
					width : 640,
					height : 410,
					layout : "border",
					border : false,
					items : [a, {
								region : "west",
								split : true,
								header : false,
								collapsible : true,
								width : 160,
								layout : "accordion",
								items : [projectRepertory,totalRepertory]
							}, d]
				});
		var sm = new Ext.grid.CheckboxSelectionModel();
		var selectedGridPanel = new Ext.grid.GridPanel({
					id : "selectedMaterialGrid",
					title : "已选物资",
					height : 382,
					autoScroll : true,
					store : new Ext.data.ArrayStore({
								fields : ["id", "materialName",'num']
							}),
					trackMouseOver : true,
					sm : sm,
					columns : [sm, new Ext.grid.RowNumberer(), {
								header : "物资名称",
								dataIndex : "materialName"
							}]
				});
		var selectedPanel = new Ext.Panel({
			width : 200,
			region : "east",
			layout : "column",
			border : false,
			items : [new Ext.Panel({
						frame : true,
						width : 35,
						height : 430,
						layout : {
							type : "vbox",
							pack : "center",
							align : "stretch"
						},
						defaults : {
							margins : "0 0 5 0"
						},
						items : [{
									xtype : "button",
									text : "&gt;&gt;",
									handler : function() {
										var materialProjectSelectorGrid = Ext.getCmp("MaterialProjectSelectorGrid");
										var selectedMaterialGrid = Ext.getCmp("selectedMaterialGrid");
										var selectedMaterialStore = selectedMaterialGrid.getStore();
										var selections = materialProjectSelectorGrid.getSelectionModel().getSelections();
										for (var p = 0; p < selections.length; p++) {
											var id = selections[p].data.id;
											var materialName =  selections[p].data.materialName;
											var num = selections[p].data.num;
											var bool = false;
											for (var i = 0; i < selectedMaterialStore.getCount(); i++) {
												if (selectedMaterialStore.getAt(i).data.id == id) {
													bool = true;
													break;
												}
											}
											if (!bool) {
												var record = {
													id : id,
													materialName : materialName,
													num:num
												};
												var r = new selectedMaterialStore.recordType(record);
												selectedMaterialGrid.stopEditing();
												selectedMaterialStore.add(r);
											}
										}
									}
								}, {
									xtype : "button",
									text : "&lt&lt;",
									handler : function() {
										var selectedMaterialGrid = Ext.getCmp("selectedMaterialGrid");
										var selections = selectedMaterialGrid.getSelectionModel().getSelections();
										var store = selectedMaterialGrid.getStore();
										for (var i = 0; i < selections.length; i++) {
											selectedMaterialGrid.stopEditing();
											store.remove(selections[i]);
										}
									}
								}]
					}), selectedGridPanel]
		});
		if (!flag) {
			eastPanel.add(selectedPanel);
		}
		return eastPanel;
	}
};